import java.awt.Component;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;

import javax.swing.BoxLayout;
import javax.swing.JLabel;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.SwingUtilities;


public class Counter extends JPanel {
	private JLabel name, count;
	private Counter thisCounter = this;
	public Counter(String name) {
		setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
		this.name = new JLabel(name);
		this.name.setAlignmentX(0.5f);
		count = new JLabel("0");
		count.setAlignmentX(0.5f);
		add(this.name);
		add(count);
		PopClickListener pl = new PopClickListener(this);
		addMouseListener(pl);
		addMouseMotionListener(pl);
	}
	
	public int getValue(){
		return Integer.parseInt(count.getText());
	}
	
	public void addLogListener(LogListener pLL){
		this.listenerList.add(LogListener.class,pLL);
	}
	
	/**
	 * fires off the given event to all LogListeners
	 * @param pAE
	 */
	private void fireEvent(ActionEvent pAE){
		LogListener[] help = this.getListeners(LogListener.class);
		for(int i=0;i<help.length;i++){
			help[i].actionPerformed(pAE);
		}
	}
	
	public void increment(int number) {
		count.setText(""+(Integer.parseInt(count.getText()) + number));
		setBounds(getX(), getY(), getMinimumSize().width, getMinimumSize().height);
	}
	
	class PopUpMenu extends JPopupMenu {
	    JMenuItem aItem, bItem;
	    public PopUpMenu(Component c){
	        aItem = new JMenuItem("Increment");
	        aItem.addActionListener(new CounterActionListener(c));
	        bItem = new JMenuItem("Decrement");
	        bItem.addActionListener(new CounterActionListener(c));
	        add(aItem);
	        add(bItem);
	    }
	}

	class PopClickListener implements MouseMotionListener, MouseListener {
		private Component moveComponent;
		boolean dragged;
		private int x, y;
		public PopClickListener(Component c) {
			moveComponent = c;
			dragged = false;
		}
	    public void mousePressed(MouseEvent e){	
	    	if (e.isPopupTrigger())
	    		doPop(e);
	    	else {
	    		dragged = true;
	    		x = e.getX();
	    		y = e.getY();
	    	}
	    }

	    public void mouseReleased(MouseEvent e){
	        if (e.isPopupTrigger())
	            doPop(e);
	        Point p = SwingUtilities.convertPoint(thisCounter, x, y, Game.getGame().cardtable);
	        Game.getGame().client.sendMoveObject(name.getText(), (int) p.getX()-x, (int) p.getY()-y);
	        dragged = false;
	    }

	    private void doPop(MouseEvent e){
	        PopUpMenu menu = new PopUpMenu(moveComponent);
	        menu.show(e.getComponent(), e.getX(), e.getY());
	    }
		@Override
		public void mouseDragged(MouseEvent e) {
			if (dragged) {
				//moveComponent.setBounds(moveComponent.getX()+e.getX()-x,moveComponent.getY()+e.getY()-y, moveComponent.getWidth(), moveComponent.getHeight());
				Point p = SwingUtilities.convertPoint(thisCounter, e.getX(), e.getY(), Game.getGame().cardtable);
		        Game.getGame().client.sendMoveObject(name.getText(), (int) p.getX()-x, (int) p.getY()-y);
			}
		}
		
		public void mouseMoved(MouseEvent e) {	
		}
		public void mouseClicked(MouseEvent e) {
		}
		public void mouseEntered(MouseEvent e) {
		}
		public void mouseExited(MouseEvent e) {
		}
	}

	class CounterActionListener implements ActionListener {
		private Counter c;
		public CounterActionListener(Component c) {
			this.c = (Counter) c;
		}
		public void actionPerformed(ActionEvent e) {
			if (((JMenuItem)e.getSource()).getText().equals("Increment")) {
				String retV = JOptionPane.showInputDialog("By how much:");
				try {
					Game.getGame().client.sendInc(Integer.parseInt(retV), name.getText());
				} catch (NumberFormatException ex) {
				}
			} else if (((JMenuItem)e.getSource()).getText().equals("Decrement")) {
				String retV = JOptionPane.showInputDialog("By how much:");
				try {
					Game.getGame().client.sendInc(-Integer.parseInt(retV), name.getText());
				} catch (NumberFormatException ex) {
				}
			}
		}		
	}
}


